/**
 *	@file	pgskin_key.c
 *	@brief	スキンアプリ　キー取得
 *	@author	Miyuki Iwasawa
 *	@date	07.09.03
 */


#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/buflen.h"
#include "gflib/touchpanel.h"
#include "application/app_tool.h"
#include "application/pokegear.h"
#include "msgdata/msg.naix"

#include "../pgear_sys.h"
#include "../pgear_frm.h"
#include "../pgear_act.h"
#include "pgskin_sys.h"
#include "pgskin_bg.h"
#include "pgskin_sub.h"

#define __PGSKIN_KEY_H_GLOBAL
#include "pgskin_key.h"


/**
 *	@brief	指定ポジションのスキンが有効かチェック
 */
static BOOL skinkey_IsPosEnable(PGSKIN_SYS_WORK* wk,u8 pos)
{
	u16 flag = 1;
	flag <<= pos;
	return wk->skin_f & flag;
}

/*
 *	@brief	通常アプリキー操作
 */
int PGSkinKey_ContKey(PGSKIN_SYS_WORK* wk)
{
	u8 pos;

	if(sys.trg & PAD_BUTTON_B){	//システムキーモードに戻る
		wk->sys->control = PGCONT_SYS;
		CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_APP_PARAM1,FALSE);
		CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_SYS_PARAM,TRUE);
		CursorMvMan_SetCurrent(wk->sys->curMan,PGKM_SYS_PARAM,PGFrame_GetSCurModePosNo(wk->sys));

		Snd_SePlay(SE_PGEAR_CANCEL);
		return PG_NEXT_NULL;
	}
	if(sys.trg & PAD_BUTTON_A){	//決定ボタン
		pos = CursorMvMan_GetPos(wk->sys->curMan);
		if(!skinkey_IsPosEnable(wk,pos)){
			return PG_NEXT_NULL;
		}
		PGSkinSub_SelBoxSet(wk,pos);
		Snd_SePlay(SE_PGEAR_DECIDE);
		return PG_NEXT_APP_SEQ;
	}
	//通常キー操作
	if(sys.trg & PAD_KEY_LEFT){
		CursorMvMan_MovePos(wk->sys->curMan,CURMV_DIR_LEFT);
		Snd_SePlay(SE_PGEAR_MOVE_CURSOR);
	}else if(sys.trg & PAD_KEY_RIGHT){
		CursorMvMan_MovePos(wk->sys->curMan,CURMV_DIR_RIGHT);
		Snd_SePlay(SE_PGEAR_MOVE_CURSOR);
	}else if(sys.trg & PAD_KEY_UP){
		CursorMvMan_MovePos(wk->sys->curMan,CURMV_DIR_UP);
		Snd_SePlay(SE_PGEAR_MOVE_CURSOR);
	}else if(sys.trg & PAD_KEY_DOWN){
		CursorMvMan_MovePos(wk->sys->curMan,CURMV_DIR_DOWN);
		Snd_SePlay(SE_PGEAR_MOVE_CURSOR);
	}

	return PG_NEXT_NULL;
}

/**
 *	@brief	通常アプリタッチ操作
 */
int PGSkinKey_ContTouch(PGSKIN_SYS_WORK* wk)
{
	int ret;
	static const RECT_HIT_TBL Btn_TpRect[] = {
//		{0,191,0,255}, ty,by,lx,rx
		{SM01_DPY,SM01_DPY+SM_DSY,SM01_DPX,SM01_DPX+SM_DSX},
		{SM02_DPY,SM02_DPY+SM_DSY,SM02_DPX,SM02_DPX+SM_DSX},
		{SM03_DPY,SM03_DPY+SM_DSY,SM03_DPX,SM03_DPX+SM_DSX},
		{SM04_DPY,SM04_DPY+SM_DSY,SM04_DPX,SM04_DPX+SM_DSX},
		{SM05_DPY,SM05_DPY+SM_DSY,SM05_DPX,SM05_DPX+SM_DSX},
		{SM06_DPY,SM06_DPY+SM_DSY,SM06_DPX,SM06_DPX+SM_DSX},
		{RECT_HIT_END,0,0,0}
	};

	ret = PGFrame_TpControl(wk->sys);
	if(ret != PG_NEXT_NULL){
		return ret;
	}

	ret = GF_TP_RectHitTrg(Btn_TpRect);
	if(	ret != RECT_HIT_NONE){
//		GF_BGL_DotCheck(wk->sys->bgl,GF_BGL_FRAME1_M,sys.tp_x,sys.tp_y,&pat) == FALSE){
		if(!skinkey_IsPosEnable(wk,ret)){
			return PG_NEXT_NULL;
		}
		if(wk->sys->control == PGCONT_SYS){
			PGSkinSub_ToTouchChange(wk);	//カーソルをアプリモードに
		}

		CursorMvMan_SetPos(wk->sys->curMan,ret);
		PGSkinSub_SelBoxSet(wk,ret);
		Snd_SePlay(SE_PGEAR_DECIDE);
		wk->sys->key_mode = APP_KTST_TOUCH;
		return PG_NEXT_APP_SEQ;
	}

	return PG_NEXT_NULL;
}

